package interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author fei
 * @date 2023/1/10 11:05
 * @description 后手案例
 */
public class InterruptDemo3 {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t" +
                            "中断标志位: " + Thread.currentThread().isInterrupted() + "程序停止");
                    break;
                }
                System.out.println("-----hello InterruptDemo3");
                try {
                    TimeUnit.MILLISECONDS.sleep(2000);
                } catch (InterruptedException e) {
                    // 没有它，程序不会停止，中断不打断
                    // 重新让 中断标志位 设置为 true
                    // Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
            }
        }, "t1");
        t1.start();

        try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }
        new Thread(() -> t1.interrupt(), "t2").start();
    }

}
